package vip.xiaonuo.modular.facegrouporgrelate.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import vip.xiaonuo.facedoor.modular.memberuser.mapper.MemberUserMapper;
import vip.xiaonuo.facedoor.modular.memberuser.param.MemberUserParam;
import vip.xiaonuo.facedoor.modular.memberuser.vo.MemberUserPageVO;
import vip.xiaonuo.modular.facegrouporgrelate.entity.FacegroupOrgRelate;
import vip.xiaonuo.modular.facegrouporgrelate.param.FacegroupOrgRelateParam;
import vip.xiaonuo.modular.grouporgrelatechangetemp.entity.GroupOrgRelateChangeTemp;
import vip.xiaonuo.modular.grouporgrelatechangetemp.service.GroupOrgRelateChangeTempService;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class FaceGroupOrgRelateSaveService {

    @Resource
    private FacegroupOrgRelateService relateService;

    @Resource
    private GroupOrgRelateChangeTempService tempService;

    @Resource
    private MemberUserMapper userMapper;

    /**
     * 保存新增
     * @param addIds
     * @param groupId
     */
    public void saveAdd(@NotNull List<Long> addIds , Long groupId){
        List<FacegroupOrgRelate> toSave = new ArrayList<>();
        List<GroupOrgRelateChangeTemp> temps = new ArrayList<>();
        for(Long id : addIds){
            FacegroupOrgRelate relate = new FacegroupOrgRelate();
            relate.setOrgId(id);
            relate.setFaceGroupId(groupId);
            toSave.add(relate);

            // 获取机构下的所有人员
            List<MemberUserPageVO> users = getUsersByOrgId(id);
            for( MemberUserPageVO user : users){
                GroupOrgRelateChangeTemp temp = new GroupOrgRelateChangeTemp();
                temp.setUserId(user.getId());
                temp.setFaceGroupId(groupId);
                temp.setStatus(0);
                temp.setBizType(0);
                temp.setTimes(0);
                temps.add(temp);
            }
        }
        relateService.saveBatch(toSave);
        // 保存中间表数据
        tempService.saveBatch(temps);
    }

    public void removeDel(@NotNull List<Long> delIds , Long groupId){
        List<FacegroupOrgRelate> toDel = new ArrayList<>();
        List<GroupOrgRelateChangeTemp> temps = new ArrayList<>();
        for(Long id : delIds){
            FacegroupOrgRelateParam param = new FacegroupOrgRelateParam();
            param.setOrgId(id);
            param.setFaceGroupId(groupId);
            List<FacegroupOrgRelate> toRemove = relateService.list(param);
            toDel.addAll(toRemove);

            // 获取机构下的所有人员
            List<MemberUserPageVO> users = getUsersByOrgId(id);
            for( MemberUserPageVO user : users){
                GroupOrgRelateChangeTemp temp = new GroupOrgRelateChangeTemp();
                temp.setUserId(user.getId());
                temp.setFaceGroupId(groupId);
                temp.setStatus(0);
                temp.setBizType(1);
                temp.setTimes(0);
                temps.add(temp);
            }
        }
        relateService.removeByIds(toDel);
        // 保存中间表数据
        tempService.saveBatch(temps);
    }


    private List<MemberUserPageVO> getUsersByOrgId (Long id){
        MemberUserParam param = new MemberUserParam();
        param.setOrgId(id);
        return userMapper.findList(param);
    }

    /**
     * 获取变动的数据
     * @param base 基础数据
     * @param current 修改后的数据
     * @return
     */
    public List<Long> getDiff(@NotNull List<Long> base, @NotNull List<Long> current){
        List<Long> result = new ArrayList<>();
        for (Long c : current){
            if(!base.contains(c)){
                result.add(c);
            }
        }
        return result;
    }

}
